package com.gyf.usercenter.ui.activity

import android.os.Bundle
import android.view.View
import com.gyf.base.common.AppManager
import com.gyf.base.ext.enable
import com.gyf.base.ext.onClick
import com.gyf.base.ui.activity.BaseMvpActivity
import com.gyf.base.widget.VerifyButton
import com.gyf.usercenter.R
import com.gyf.usercenter.injection.component.DaggerUserComponent
import com.gyf.usercenter.injection.module.UserModule
import com.gyf.usercenter.ui.presenter.RegisterPresenter
import com.gyf.usercenter.ui.presenter.view.RegisterView
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.toast

class RegisterActivity : BaseMvpActivity<RegisterPresenter>(), RegisterView, View.OnClickListener {

    private var pressTime: Long = 0;
    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(activityComponent).userModule(UserModule()).build().inject(this);
        mPresenter.mView = this;
    }

    override fun onRegisterResult(result: String) {
        toast(result);
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        initView();
    }

    private fun initView() {
        mRegisterBtn.enable(mVerifyCodeEt) { isBtnEnable() };
        mRegisterBtn.enable(mRegisterMobileEt) { isBtnEnable() };
        mRegisterBtn.enable(mRegisterPwdEt) { isBtnEnable() };
        mRegisterBtn.enable(mPwdConfirmEt) { isBtnEnable() };

        mVerifyCodeBtn.setOnVerifyBtnClick(object : VerifyButton.OnVerifyBtnClick {
            override fun onClick() {
                toast("获取验证码");
            }
        })
        mVerifyCodeBtn.setOnClickListener(this);
        mRegisterBtn.setOnClickListener(this);

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.mVerifyCodeBtn -> {
                mVerifyCodeBtn.requestSendVerifyNumber();
            }
            R.id.mRegisterBtn -> {
                mPresenter.register(mRegisterMobileEt.text.toString(), mRegisterPwdEt.text.toString(), mVerifyCodeEt.text.toString());
            }
        }
    }

    private fun isBtnEnable(): Boolean {
        return mRegisterMobileEt.text.isNullOrEmpty().not() &&
                mVerifyCodeEt.text.isNullOrEmpty().not() &&
                mRegisterPwdEt.text.isNullOrEmpty().not() &&
                mPwdConfirmEt.text.isNullOrEmpty().not();
    }

    override fun onBackPressed() {
        val time = System.currentTimeMillis();
        if (time - pressTime > 2000) {
            toast("双击退出");
            pressTime = time;
        } else {
            AppManager.instance.exitApp(this);
        }
    }
}
